﻿@{
    ViewBag.Title = "Form"; 
    Layout = "~/Views/Shared/_Form.cshtml";
}
    <script>
        function uuid() {
            var s = [];
            var hexDigits = "0123456789abcdef";
            for (var i = 0; i < 36; i++) {
                s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
            }
            s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
            s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
            s[8] = s[13] = s[18] = s[23] = "-";

            var uuid = s.join("");
            return uuid;
        }
        layui.use(['jquery', 'form', 'laydate', 'tablePlug', 'common', 'xmSelect'], function () {
            var form = layui.form,
                $ = layui.$,
                common = layui.common,
                xmSelect = layui.xmSelect,
                tablePlug = layui.tablePlug;
            var keyValue = $.request("keyValue");
            var module = $.request("module");
            var cout = 0;
            $(function () {
                initControl();
                if (!!keyValue) {
                    var tempdata = parent.layui.table.cache.currentTableId;
                    var Filters = [];
                    for (var i = 0; i < tempdata.length; i++) {
                        if (keyValue == tempdata[i].F_Id) {
                            Filters = JSON.parse(tempdata[i].Filters);
                            $('#Description').val(tempdata[i].Description);
                            break;
                        }
                    }
                    for (var i = 0; i < Filters.length; i++) {
                        $('#NF-add').click();
                        $('#fields' + i).val(Filters[i].Key);
                        $('#symbol' + i).val(Filters[i].Contrast);
                        $('#lookname' + i).val(Filters[i].Text);
                        $('#values' + i).val(Filters[i].Value);
                    }
                    form.render();
                }
                else {
                    $('#NF-add').click();
                    form.render();
                }
                form.render();
            });
            function initControl() {
                $("#fields").bindSelect({
                    url: "/SystemManage/ModuleFields/GetSelectJson?moduleId=" + module,
                });
            }
            $(document).on('click', '#NF-add', function () {
                var target = $('#ruledata');
                var html = $("#divInputTemplate").prop("outerHTML");
                html = html.replace(/rulecontext/g, "rulecontext" + cout); // 替换多个
                html = html.replace(/fields/g, "fields" + cout); // 替换多个
                html = html.replace(/symbol/g, "symbol" + cout); // 替换多个
                html = html.replace(/lookname/g, "lookname" + cout); // 替换多个
                html = html.replace(/divInputTemplate/g, "divInput" + cout);
                html = html.replace(/values/g, "values" + cout);
                html = html.replace(/searchCout/g, cout);
                var obj = $(html);
                obj.removeClass("layui-hide");
                obj.appendTo(target);
                cout++;
                form.render();
            });
            //select验证
            form.verify({
                required: function (value, item) {
                    var msg = "必填项不能为空";
                    value = $.trim(value);
                    var isEmpty = !value || value.length < 1;
                    // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                    if (item.tagName == 'SELECT' && isEmpty) {
                        $("html").animate({
                            scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                        }, 50);
                    }
                    if (isEmpty) {
                        return msg;
                    }
                }
            });
            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var index = parent.layer.load(0, {
                    shade: [0.5, '#000'], //0.1透明度的背景
                });
                var postData = {};
                postData.Operation = data.field.Operation;
                postData.Description = data.field.Description;
                if (!!keyValue) {
                    postData.F_Id = keyValue;
                }
                else {
                    postData.F_Id = uuid();
                }
                var Filters = [];
                for (var i = 0; i < cout; i++) {
                    var temp = {};
                    temp["Key"] = $('#fields' + + i).val();
                    temp["Contrast"] = $('#symbol' + + i).val();
                    temp["Text"] = $('#lookname' + + i).val();
                    temp["Value"] = $('#values' + + i).val();
                    if (!temp["Value"]) {
                        temp["Value"] = temp["Text"];
                    }
                    if (!!temp["Key"]) {
                        Filters.push(temp);
                    }
                }
                postData.Filters = JSON.stringify(Filters);
                var tempdata = parent.layui.table.cache.currentTableId;
                if (!!keyValue) {
                    for (var i = 0; i < tempdata.length; i++) {
                        if (keyValue == tempdata[i].F_Id) {
                            tempdata[i].Filters = postData.Filters;
                            tempdata[i].Description = postData.Description;
                            break;
                        }
                    }
                }
                else {
                    tempdata.push(postData);
                }
                common.modalMsg("操作成功", "success");
                parent.layui.table.reload('currentTableId', {
                    data: tempdata
                });
                parent.layer.close(index);
                common.modalClose();
                return false;
            });
        });
        function deleteRule(data) {
            var obj = document.getElementById(data);//建议使用ID
            if (obj != null) {
                obj.parentNode.removeChild(obj);
            }
            layui.use(['jquery', 'form', 'laydate', 'tablePlug', 'common'], function () {
                layui.form.render();
            });
        }
        function search(data) {
            var fields = $('#fields' + data).val();
            if (fields != '{loginRole}' && fields != '{loginUser}' && fields != '{loginOrg}') {
                return false;
            }
            $('#lookname' + data).val(null);
            $('#values' + data).val(null);
            layui.use(['jquery', 'form', 'common'], function () {
                var form = layui.form,
                    $ = layui.$,
                    common = layui.common;
                //不同弹窗
                if (fields == '{loginRole}') {
                    common.modalOpen({
                        title: "选择角色",
                        url: "/SystemManage/Role/AddForm?name=" + "lookname" + data + "&value=" +"values"+data,
                        width: "650px",
                        height: "500px",
                    });
                }
                else if (fields == '{loginUser}') {
                    common.modalOpen({
                        title: "选择用户",
                        url: "/SystemManage/User/AddForm?name=" + "lookname" + data + "&value=" + "values" + data,
                        width: "650px",
                        height: "500px",
                    });
                }
                else if (fields == '{loginOrg}') {
                    common.modalOpen({
                        title: "选择组织",
                        url: "/SystemManage/Organize/AddForm?name=" + "lookname" + data + "&value=" + "values" + data,
                        width: "650px",
                        height: "500px",
                    });
                }
                else {
                    return false;
                }
            });
        }
    </script>

    <body>
        <div class="layui-input-item layui-hide" id="divInputTemplate">
            <label class="layui-form-label required">规则内容</label>
            <div class="layui-input-block" style="padding-right: 70px;">
                @*<input type="text" id="rulecontext" name="rulecontext" class="layui-input" autocomplete="off" placeholder="请输入规则">*@
                <select id="fields" name="fields" lay-verify="required" lay-search>
                    <option value="">请选择</option>
                    <option value="{loginRole}">{当前登录用户的角色}</option>
                    <option value="{loginUser}">{当前登录的用户}</option>
                    <option value="{loginOrg}">{当前登录用户的部门}</option>
                </select>
                <select id="symbol" name="symbol" lay-verify="required" lay-search>
                    <option value="">请选择</option>
                    <option value=">=">大于等于</option>
                    <option value="<=">小于等于</option>
                    <option value="==">等于</option>
                    <option value="!=">不等于</option>
                    <option value="contains">包含</option>
                    <option value="in">属于</option>
                    <option value="intersect">含有任意一个</option>
                </select>
                <input type="text" id="lookname" name="lookname" class="layui-input" lay-verify="required" value="" placeholder="请输入值">
                <input type="text" id="values" name="values" class="layui-input layui-hide">
                <button class="layui-btn layui-btn-danger" style="position: absolute;top: 0;right: 6px;cursor: pointer;" onclick="deleteRule('divInputTemplate')"><i class="layui-icon">&#xe67e;</i></button>
                <button class="layui-btn layui-btn-primary" style="position: absolute;top: 0;right: 6px;cursor: pointer;margin-top:75px" onclick="search(searchCout)">...</button>
            </div>
        </div>
        <div class="layuimini-container">
            <div class="layuimini-main">
                <blockquote class="layui-elem-quote layui-text">
                    当前登录的用户{loginUser},当前登录用户的角色{loginRole}，当前用户的部门{loginOrg}
                </blockquote>
                <div class="layui-form layuimini-form" lay-filter="adminform">
                    <div class="layui-form-item">
                        <label class="layui-form-label">规则间条件</label>
                        <div class="layui-input-block" style="padding-right: 70px;">
                            <select id="Operation" name="Operation" lay-verify="required" disabled>
                                <option value="and" selected>并且</option>
                                <option value="or">或者</option>
                            </select>
                            <button id="NF-add" class="layui-btn" style="position: absolute;top: 0;right: 6px;cursor: pointer;"><i class="layui-icon">&#xe654;</i></button>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">规则备注</label>
                        <div class="layui-input-block">
                            <textarea id="Description" name="Description" class="layui-textarea" placeholder="请输入规则备注"></textarea>
                        </div>
                    </div>
                    <div id="ruledata">
                    </div>
                    <div class="layui-form-item layui-hide">
                        <button class="layui-btn" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </body>

